<?php
/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 14/12/23
 * Time: 下午2:24
 */

class PubEtl {

    private $_etlAppId  = 'data';
    private $_host      = 'http://183.2.217.35:8081';
    private $_key       = 'thisisakeyo!@##@!data';

    public function getInfoFromEtl($url, $params, $type=null){
        if (empty($url) || empty($params)) return ;

        $user = PubAuth::getUser();
        $params['appId']    = $this->_etlAppId;
        $params['operator'] = $user['UserID'];
        $params['time']     = time();

        $urlParams = $this->getUrlParamsAndFlag($params);
        $href = $this->_host.$url.'?'.$urlParams;
        $data = file_get_contents($href);

        self::putLog($href, $data);

        if($type == 'json'){
            return CJSON::decode($data);
        }else{
            return $data;
        }
    }

    public function getUrlParamsAndFlag($params){
        if (is_array($params) && count($params)){
            $urlParams = '';
            $flagParams = '';
            ksort($params);
            foreach ($params as $key => $val){
                $val = rawurlencode($val);
                $urlParams .= "{$key}={$val}&" ;
                $flagParams .= "{$key}{$val}" ;
            }
            $flag = md5($flagParams.$this->_key);
            $urlParams .= 'flag='.$flag;
            return $urlParams;
        }
        return ;
    }

    public function getServer($platform, $game, $type='', $cache = true){
        $params = array(
            'platform'  => $platform,
            'game'      => $game,
            'type'      => $type
        );

        $data = $this->getInfoFromEtl("/etlCenter/api/listServer.htm", $params, 'json');

        $server = array();

        if(!isset($data['code'])){
            foreach($data as $v){
                $server[$v['server']] = $v['server'];
            }
        }

        return $server;
    }
    
    
    public function getGameForPl($platform=""){
    	
    	$data = $this->getInfoFromEtl("/etlCenter/api/listPlatformGame.htm", array('platform' => $platform));
    	$ret  = array();
    	if(empty(trim($data))){
    		return $ret;
    	}
    	
    	$tmpArr = explode(',', $data);
    	
    	foreach ($tmpArr as $val){
    		$ret[trim($val)] = $val;
    	}
    	
    	return $ret;
    	
    }
    
    public function getServerForPl($p, $g){
    	
    	$map = array(
    			'slg'    => 'legion',
    			'st'     => 'stormthron',
    			'winter' => 'Winterfrost Legac',
    			'hotb'   => 'htb',
    	);
    	
    	$g = isset($map[$g]) ? $map[$g] : $g;

    	$data = $this->getInfoFromEtl("/etlCenter/api/listPlatformServer.htm", array( 'platform' => $p, 'game' => $g));
    	$ret  = array();
    	
    	if(empty(trim($data))){
    		return $ret;
    	}
    	
    	$tmpArr = explode(',', $data);
    	
    	foreach ($tmpArr as $val){
    		$ret[trim($val)] = $val;	
    	}
    	
    	return $ret;
    }

    private static function putLog( $link, $response = ''){
        $time =  time();

        $log = 'Time:' . date('Y-m-d H:i:s', $time) . "\tLink:{$link}\nResponse:{$response}\n\n";

        $logDir = "/data/logs/".date('Ymd', $time);
        if(!file_exists($logDir)) {
            mkdir($logDir,0777,true);
        }

        file_put_contents($logDir.'/curl_log.txt', $log, FILE_APPEND);
    }

    public function decryptData($data){
        // 加密信息用的key，先md5，再取前16位
        $encryptKey = substr(md5($this->_key), 0, 16);

        // 解密
        // 先base64解码
        $d = base64_decode($data);

        // 解密
        $decryptData = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_128,$encryptKey,$d,MCRYPT_MODE_ECB, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"), "\x00..\x1F");

        return $decryptData;
    }

    public function encryptData($data){
        //print_r($key);exit;
        // 加密信息用的key，先md5，再取前16位
        $encryptKey = substr(md5($this->_key), 0, 16);

        // 使用AES ECB 加密
        $encryptData =  base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $encryptKey, $data, MCRYPT_MODE_ECB));

        // 加密后的数据
        return $encryptData;
    }
} 